$(function () {
  get();
  const layer = layui.layer;
  $(".btn-out").on("click", function () {
    layer.confirm("确定退出吗?", { icon: 3, title: "提示" }, function (index) {
      //do something
      localStorage.removeItem("token");
      location.href = "/login.html";
      layer.close(index);
    });
  });
});
function get() {
  $.ajax({
    url: "/my/userinfo",
    method: "GET",
    // headers: {
    //   Authorization:localStorage.getItem('token')||''
    // },
    success: function (res) {
      // console.log(res);
      if(res.status !== 0){return layer.msg('验证身份失败')}
      render(res.data);
    },

  });
}

function render(data) {
  const name = data.nickname || data.username; //获取name
  // console.log(name);
  $("#welcome").html(`欢迎${name}`); //判断是否有图片头像，有得话渲染图片头像隐藏文字头像没有则反之
  if (data.user_pic !== null) {
    $(".layui-nav-img").attr("src", data.user_pic).show();
    $(".text-avatar").hide();
  } else {
    const fiirst = name[0].toUpperCase(); //文字头像是第一个字母大写
    $(".text-avatar").html(fiirst).show();
    $(".layui-nav-img").hide();
  }
}
